package str;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class St {
    /**
     * 对任意一个Map<String, Object>, 其 key 为 String,
     * 其 value 为 Map<String, Object> Object[] Number String 中的任意一种,
     * 显然叶子节点是 value 类型为 Number 或 String的节点,
     * 将 Map 转为多条字符串, 每条字符串表达其中一个叶子节点,

     * @param map 上述的 map
     * @return 所有的字符串
     */
    public static Set<String> showMap(Map<String, Object> map) {
        //TODO your code goes here...
        Set<String> set = new HashSet<>();
        fenLei(map,null,set);

        return set;
    }
    public static void fenLei(Object o,String s, Set set){
        if (o == null)
            doNull(s,set);
        if (o instanceof String)
            doString((String) o,s,set);
        if (o instanceof Number)
            doNum((Number) o,s,set);
        if (o instanceof Object[])
            doList((Object[]) o,s,set);
        if (o instanceof Map)
            doMap((Map) o,s,set);

    }




    public static void doList(Object[] o, String s, Set set){
        for (int i = 0; i <o.length ; i++) {
            s += "["+i+"]";
            fenLei(o,s,set);
        }
    }
    public static void doMap(Map map,String s,Set set){
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
            String key = (String)entry.getKey();
            Object value = entry.getValue();
            if (s == null){
                s=key;
            }else {
                s+="."+key;
            }

            fenLei(value,s,set);
        }
    }
    public static void doNum(Number o, String s, Set set){
        set.add(s+" = "+o);
    }
    public static void doString(String o, String s, Set set){
        set.add(s+" + "+o);
    }
    private static void doNull(String s, Set set) {
        set.add(s+" = "+"null");
    }
}
